using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using ComponentFactory.Krypton.Toolkit;
using Crownwood.Magic.Common;
using TinyERP.Framework.Controls;
using TinyERP.Framework;
using Bamboo.Core.Data;
using TinyERP.Framework.Plugin;
using System.Globalization;
using System.Threading;

namespace TinyERPApp
{
    public partial class MainScreen : KryptonForm
    {
        private IDictionary<string, PaletteModeManager> themeDictionary;
        private ApplicationMenu menu;
        private ComponentFactory.Krypton.Toolkit.KryptonManager uiManager;
        private ComponentFactory.Krypton.Toolkit.KryptonPanel navigationPanel;
        private ComponentFactory.Krypton.Toolkit.KryptonPanel mainPanel;
        
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            ComponentFactory.Krypton.Toolkit.KryptonSplitContainer contentContainer;
            this.navigationPanel = new ComponentFactory.Krypton.Toolkit.KryptonPanel();
            this.mainPanel = new ComponentFactory.Krypton.Toolkit.KryptonPanel();
            
            this.uiManager = new ComponentFactory.Krypton.Toolkit.KryptonManager(this.components);
            contentContainer = new ComponentFactory.Krypton.Toolkit.KryptonSplitContainer();
            ((System.ComponentModel.ISupportInitialize)(contentContainer)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(contentContainer.Panel1)).BeginInit();
            contentContainer.Panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(contentContainer.Panel2)).BeginInit();
            contentContainer.Panel2.SuspendLayout();
            contentContainer.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.navigationPanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.mainPanel)).BeginInit();

            this.SuspendLayout();
            // 
            // contentContainer
            // 
            contentContainer.Cursor = System.Windows.Forms.Cursors.Default;
            contentContainer.Dock = System.Windows.Forms.DockStyle.Fill;
            contentContainer.Location = new System.Drawing.Point(0, 24);
            contentContainer.Name = "contentContainer";
            // 
            // contentContainer.Panel1
            // 
            contentContainer.Panel1.Controls.Add(this.navigationPanel);
            contentContainer.Panel1MinSize = 50;
            // 
            // contentContainer.Panel2
            // 
            contentContainer.Panel2.Controls.Add(this.mainPanel);
            contentContainer.Size = new System.Drawing.Size(850, 427);
            contentContainer.SplitterDistance = 135;
            contentContainer.TabIndex = 1;
            // 
            // navigationPanel
            // 
            this.navigationPanel.AutoSize = true;
            this.navigationPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.navigationPanel.Location = new System.Drawing.Point(0, 0);
            this.navigationPanel.Name = "navigationPanel";
            this.navigationPanel.Size = new System.Drawing.Size(135, 427);
            this.navigationPanel.TabIndex = 0;
            // 
            // mainPanel
            // 
            this.mainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.mainPanel.Location = new System.Drawing.Point(0, 0);
            this.mainPanel.Name = "mainPanel";
            this.mainPanel.PanelBackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.PanelAlternate;
            this.mainPanel.Size = new System.Drawing.Size(710, 427);
            this.mainPanel.TabIndex = 0;

            // 
            // MainScreen
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(850, 451);
            this.Controls.Add(contentContainer);
            this.Name = "MainScreen";
            this.Text = "TinyERP";
            ((System.ComponentModel.ISupportInitialize)(contentContainer.Panel1)).EndInit();
            contentContainer.Panel1.ResumeLayout(false);
            contentContainer.Panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(contentContainer.Panel2)).EndInit();
            contentContainer.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(contentContainer)).EndInit();
            contentContainer.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.navigationPanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.mainPanel)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        
        public MainScreen()
        {
            InitializeComponent();
            LoadConfiguration();
            LoadPlugins();
            LoadLocale();
            InitNavigationPanel();
            InitMainPanel();
            WireGlobalKeyEvents();
            LoadDefaultTheme();
            BuildMenu();
        }
        private void LoadLocale()
        {
            string language = ApplicationConfiguration.GetInstance().DefaultLanguage;
            CultureInfo cultureInfo = new CultureInfo(language);
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }
        private void BuildMenu()
        {
            this.menu = new ApplicationMenu();
            this.menu.Font = new System.Drawing.Font("Segoe UI", 8.25F);
            this.menu.Location = new System.Drawing.Point(0, 0);
            this.menu.Name = "menu";
            this.menu.Size = new System.Drawing.Size(850, 24);
            this.menu.TabIndex = 0;
            this.menu.OnChangeTheme += new OnChangeThemeHandler(OnChangeTheme);
            this.Controls.Add(this.menu);
            this.MainMenuStrip = this.menu;
        }

        private void OnChangeTheme(string themeKey)
        {
            ApplicationConfiguration.GetInstance().DefaultTheme = themeKey;
            PaletteModeManager theme = themeDictionary[ApplicationConfiguration.GetInstance().DefaultTheme];
            uiManager.ResetGlobalPaletteMode();
            uiManager.ResetGlobalPalette();
            uiManager.ResetGlobalAllowFormChrome();
            uiManager.GlobalPaletteMode = theme;
        }

        private void LoadDefaultTheme()
        {
            BuildThemeDictionary();
            uiManager.GlobalAllowFormChrome = true;
            uiManager.GlobalApplyToolstrips = true;
            PaletteModeManager theme = themeDictionary[ApplicationConfiguration.GetInstance().DefaultTheme];
            uiManager.GlobalPaletteMode = theme;
        }

        private void BuildThemeDictionary()
        {
            themeDictionary = new Dictionary<string, PaletteModeManager>();
            themeDictionary.Add("Office2007Black", PaletteModeManager.Office2007Black);
            themeDictionary.Add("Office2007Blue", PaletteModeManager.Office2007Blue);
            themeDictionary.Add("Office2007Silver", PaletteModeManager.Office2007Silver);
            themeDictionary.Add("ProfessionalOffice2003", PaletteModeManager.ProfessionalOffice2003);
            themeDictionary.Add("ProfessionalSystem", PaletteModeManager.ProfessionalSystem);
            themeDictionary.Add("SparkleBlue", PaletteModeManager.SparkleBlue);
            themeDictionary.Add("SparkleOrange", PaletteModeManager.SparkleOrange);
            themeDictionary.Add("SparklePurple", PaletteModeManager.SparklePurple);
        }

        private void LoadPlugins()
        {
            PluginManager.GetInstance().LoadPlugins();
        }

        private void LoadConfiguration()
        {
            ITinyERPConfigProvider configurationProvider = new XmlConfigurationProvider(Assembly.GetEntryAssembly(),
    "TinyERPApp.ApplicationConfig.Application.xml");
            configurationProvider.LoadConfiguration(ApplicationConfiguration.GetInstance());

        }
        private void WireGlobalKeyEvents()
        {
            this.KeyPreview = true;
            this.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                ShortcutHandler.GetInstance().HandleEvent(e);
            };
        }

        
        protected void InitNavigationPanel()
        {
            Control functionNavigator = UIManager.GetInstance().CreateNavigator();
            functionNavigator.Dock = DockStyle.Fill;
            navigationPanel.Controls.Add(functionNavigator);
        }


        protected void InitMainPanel()
        {
            Control tabControl = UIManager.GetInstance().CreateContentPane();
            mainPanel.Controls.Add(tabControl);
        }
    }
}
